home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Beginning Mac Programming
/
Beginning Mac Programming.bin
/
Open Me for REALbasic 3
/
REALbasic 3.2
/
Example Projects
/
Applications
/
Simon 1.0
/
source text
< prev
Wrap
Text File
|
1998-05-13
|
6KB
|
313 lines
Main.blink:
Sub blink(x as oval)
// Makes the Music button blink when hit by the user
// There are probably better ways to do this
// But this one is OK for this game
dim tmp as color
dim height as integer
// record current bordercolor of Music button
tmp = x.bordercolor
// select note according to the Music button number
height = x.index*4+60
// play note
note.playNote height,127
// change bordercolor to white
x.bordercolor = rgb(255,255,255)
// absolutely required for correct visual effect
x.refresh
// just for eye's catching
wait 8
// restore previous color
x.bordercolor = tmp
// absolutely required for correct visual effect
x.refresh
// stop playing note
note.playNote height,0
End Sub
Main.init:
Sub init()
// Initialisation method
dim i as integer
//creation of game buttons
for i = 0 to 5
simon(i) = new oval1
next
// Buttons are organised as an hexagon
// Their colors are the 3 primary and
// the 3 secondary ones
// First one is blue
simon(0).fillcolor = rgb(0,0,255)
simon(0).top = 30
simon(0).left = 135
// Second one is Magenta
simon(1).fillcolor = rgb(255,0,255)
simon(1).top = 80
simon(1).left = 222
// Third one is Red
simon(2).fillcolor = rgb(255,0,0)
simon(2).top = 180
simon(2).left = 222
// Fourth one is Yellow
simon(3).fillcolor = rgb(255,255,0)
simon(3).top = 230
simon(3).left = 135
// Fifth one is Green
simon(4).fillcolor = rgb(0,255,0)
simon(4).top = 180
simon(4).left = 48
// Sixth one is Cyan
simon(5).fillcolor = rgb(0,255,255)
simon(5).top = 80
simon(5).left = 48
// Max length of music sequence
numseq = 0
PlayInProgress = false
End Sub
Main.Start:
Sub Start()
// Method activated by depressing NewPlay Button
dim i as integer
// Initialisation of a random musical sequence
// 20 notes in sequence is already pretty hard
// to remember (at least for me !)
for i = 0 to 19
seq(i) = floor(rnd * 6)
next
// A new game has started
PlayInProgress =true
// No success yet
Score = 0
UpdScore Score
// Start of Sequence
Numseq = 0
CurrSeq = 0
// Play First note
PlaySeq 0
End Sub
Main.wait:
Sub wait(x as integer)
// Just my own implementation of HyperCard
// wait n x ticks
// Didn't find it in the documentation
dim t as integer
t = ticks
do
loop until ((ticks-t) > x)
End Sub
Main.PlaySeq:
Sub PlaySeq(num as integer)
// Plays a whole sequence of notes
// while showing buttons blink
dim i as integer
for i = 0 to num
blink simon(seq(i))
wait 20
next
End Sub
Main.check:
Function check(btn as integer) As Boolean
// Checks if the button hit is the right one
// in the sequence
return (btn = seq(CurrSeq))
End Function
Main.good:
Sub good()
// Button hit is the right one
if CurrSeq >= numseq then
// We have completed the right music sequence
ouais.play // French yeah :^)
// Didn't find something like Hypercard
// wait until the sound is done
wait 100
// increase score
score = CurrSeq +1
// Update score
UpdScore score
// increase sequence number
numseq = numseq+1
// Resets current index for the next sequence
CurrSeq = 0
// Have we reach the maximum score ?
if numseq = 20 then
// Play another sound
// NB : I borrowed this one to Civ II
fin.Play
// Game is over
PlayInProgress = false
numseq = 0
else
// Play the next music sequence
PlaySeq numseq
end if
else
// We are in the middle of a sequence
CurrSeq = CurrSeq+1
end if
End Sub
Main.bad:
Sub bad()
// You loose !!
iark.play
// Game is over
PlayInProgress = false
End Sub
Main.UpdScore:
Sub UpdScore(n as integer)
// Update of current score in main window
// Both, in an EditField in the center of
// the window
affscore.text = str(n)
// And in a scrollbar just below the EditField
// Quite unuseful but I just tried it and
// it worked from the first time. Coool ! :^)
scorebar.value = n
End Sub
Main.MouseDown:
Function MouseDown(X As Integer, Y As Integer) As Boolean
// Main event to be tracked down in the main window
// Detects music buttons hits and reacts accordingly
dim i, Button as integer
dim ok as boolean
// temporary object
dim tg as oval
// Button is initialised to nothing
Button = -1
if PlayInProgress then
// we check if the mouse location is within
// each music button rectangle
for i = 0 to 5
tg = simon(i)
ok = (X > tg.left) and (X < (tg.left + tg.width))
ok = ok and (Y > tg.top) and (y < (tg.top + tg.height))
if ok then
// We hit a Music Button so we give
// feedback to the user
blink tg
// and record the button number
Button = i
end if
next
// At the end ofthis loop we should have
// either one music button ( 0 to 5)
// or no button (-1)
if Button <> -1 then
// One music button has been hit
// Is it the right one ?
if check(Button) then
// Yeah got it!
good
else
// Rats !!
bad
end if
// Oops !! clicked outside all Music Buttons
end if
else
// No game is in progress so we just beep
beep
end if
End Function
Main.Close:
Sub Close()
// This application has no multidocument
// capability, so I quit upon close
quit
End Sub
Main.EnableMenuItems:
Sub EnableMenuItems()
AppleAbout.enabled = true
FileQuit.enabled =true
End Sub
Main.Open:
Sub Open()
// Main Window is not shown during initialisation
main.hide
init
main.show
End Sub
Main.bye.Action:
Sub Action()
// User has depressed 'Quit' button
quit
End Sub
Main.np.Action:
Sub Action()
// User has depressed 'New Game' button
start
End Sub
About.MouseDown:
Function MouseDown(X As Integer, Y As Integer) As Boolean
Close
End Function
About.KeyDown:
Function KeyDown(Key As String) As Boolean
Close
End Function